package frist50;

import org.junit.jupiter.api.Test;

public class T0034 {
    public int[] searchRange(int[] nums, int target) {
        int len = nums.length;
        if (len == 0) return new int[]{-1, -1};
        int left = find(nums, target, true);
        int right = find(nums, target, false)-1;
        if (left <= right && right < nums.length && nums[left] == target && nums[right] == target)return new int[]{left, right};
        return new int[]{-1, -1};
    }


    public int find(int[] nums, int target, boolean isEque) {
        int left = 0, right = nums.length - 1;
        int ans = nums.length;
        while (left <= right) {
            int mod = left + (right - left) / 2;
            if (nums[mod] > target || isEque && nums[mod] == target) {
                right = mod - 1;
                ans = mod;
            } else {
                left = mod + 1;
            }
        }
        return ans;
    }

    @Test
    public void test() {

        int[] ints = {5, 7, 7, 8, 8};
        int[] re = searchRange(ints, 8);
        System.out.println(re[0] + "  " + re[1]);
    }
}
